﻿#include"game.h"

void InitBoard()
{
	int i;
	int j;
	for (i = 0;i < ROW;i++)
	{
		for (j = 0;j < COL;j++)
		{
			board[i][j] = ' ';
		}
	}
}

void TyBoard()
{
	int i;
	int j;
	for (i = 0;i < ROW;i++)
	{
		for (j = 0;j < COL;j++)
		{
			printf(" %c ", board[i][j]);
			if (j < COL - 1)
			{
				printf("|");
			}
		}
		printf("\n");
		if (i < ROW - 1)
		{
			for (j = 0;j < COL;j++)
			{
				printf("---");
				if (j < COL - 1)
				{
					printf("|");
				}
			}
			printf("\n");
		}
	}
}

//玩家走
void Playermove()
{
	int x = 0;
	int y = 0;
	printf("玩家走:>\n");
	while (1)
	{
		printf("玩家输入坐标：");
		scanf("%d %d", &x, &y);
		if (x >= 1 && x <= ROW && y >= 1 && y <= COL)
		{
			if (board[x-1][y-1] == ' ')
			{
				board[x-1][y-1] = '*';
				break;
			}
			else
			{
				printf("坐标被占用!\n");
			}
		}
		else
		{
			printf("坐标错误，请重新输入!\n");
		}
	}
}

//电脑走
void Computemove(int nan)
{
	int i, j, count = 0;
	int sig = 0;
	int i2 = 6, j2 = 6;
	int m = 0;
	int n = 0;
	int num = 0;
    int sum = 0;
	srand((unsigned int)time(NULL));
	printf("电脑走：>\n");
	switch (nan)
	{
		//难度1 简单
	case 1:
		do
		{
			i = rand() % ROW;
			j = rand() % COL;
			if (board[i][j] == ' ')
			{
				board[i][j] = '#';
				break;
			}
		} while (1);
		break;
		//难度2 困难
	case 2:
		if (board[ROW/2][COL/2] == ' ')
		{
			board[ROW/2][COL/2] = '#';
			goto done2;
		}
	nan2:
		for (i = 0;i < ROW;i++)
		{
			m = 0;
			n = 0;
			for (j = 0;j < COL;j++)
			{
				m += board[i][j];
				n += board[j][i];
			}
			//行
			if (m == '#' * (ROW-1) + ' ')
			{
				for (j = 0;j < COL;j++)
				{
					if (board[i][j] == ' ')
					{
						board[i][j] = '#';
						goto done2;
					}
				}
			}
			if (m == '*' * (ROW-1) + ' ')
			{
				sig++;
				i2 = i;
			}
			//列
			if (n == '#' * (ROW-1) + ' ')
			{
				for (j = 0;j < ROW;j++)
				{
					if (board[j][i] == ' ')
					{
						board[j][i] = '#';
						goto done2;
					}
				}
			}
			if (n == '*' * (ROW-1) + ' ')
			{
				sig++;
				j2 = i;
			}
		}
		if (sig)
		{
			if (i2 < ROW)
			{
				for (j = 0;j < COL;j++)
				{
					if (board[i2][j] == ' ')
					{
						board[i2][j] = '#';
						goto done2;
					}
				}
			}
			else
			{
				for (j = 0;j < ROW;j++)
				{
					if (board[j][j2] == ' ')
					{
						board[j][j2] = '#';
						goto done2;
					}
				}
			}
		}
		//斜对角
		sum = 0;
		num = 0;
		for (i = 0;i < ROW;i++)
		{
			sum += board[i][i];
		}
		for (i = 0;i < ROW;i++)
		{
			num += board[i][ROW-i-1];
		}
		
			if (sum == '#' * (ROW-1) + ' ' || sum == '*' * (ROW-1) + ' ')
			{
				for (j = 0;j < ROW;j++)
				{
					if (board[j][j] == ' ')
					{
						board[j][j] = '#';
						goto done2;
					}
				}
			}
		if (num == '#' * (ROW-1) + ' ' || num == '*' * (ROW-1) + ' ')
		{
			for (j = 0;j < ROW;j++)
			{
				if (board[j][ROW - j - 1] == ' ')
				{
					board[j][ROW - j - 1] = '#';
					goto done2;
				}
			}
		}
		do
		{
			i = rand() % ROW;
			j = rand() % COL;
			if (board[i][j] == ' ')
			{
				board[i][j] = '#';
				goto done2;
			}
		} while (1);
	done2:
		break;
		//难度3 大师
		case 3:
			if (board[ROW / 2][ROW / 2] == ' ')
			{
				board[ROW / 2][ROW / 2] = '#';
				goto done3;
			}
			else
			{
				for (i = 0; i < ROW; i++)
				{
					for (j = 0; j < COL; j++)
					{
						count += board[i][j];
					}
				}
				if ((count == '*' + 8 * ' ') &&board[ROW/2][ROW/2] == '*')
				{
					board[0][0] = '#';
					goto done3;
				}
				if ((count == '*' * 2 + '#' * 1 + ' ' * 6) && board[2][2] == '*' && board[1][1] == '*')
				{
					board[0][2] = '#';
					goto done3;
				}
				else if ((count == '*' * 2 + '#' * 1 + ' ' * 6) && board[0][0] == '*' && board[2][1] == '*' && board[1][1] == '#')
				{
					board[2][0] = '#';
					goto done3;
				}
				else if ((count == '*' * 2 + '#' * 1 + ' ' * 6) && board[2][2] == '*' && board[0][1] == '*' && board[1][1] == '#')
				{
					board[0][2] = '#';
					goto done3;
				}
				else if ((count == '*' * 2 + '#' * 1 + ' ' * 6) && board[2][0] == '*' && board[0][1] == '*' && board[1][1] == '#')
				{
					board[0][0] = '#';
					goto done3;
				}
				else if ((count == '*' * 2 + '#' * 1 + ' ' * 6) && board[0][2] == '*' && board[2][1] == '*' && board[1][1] == '#')
				{
					board[2][2] = '#';
					goto done3;
				}
				else if ((count == '*' * 2 + '#' * 1 + ' ' * 6) && board[1][0] == '*' && board[0][1] == '*' && board[1][1] == '#')
				{
					board[0][0] = '#';
					goto done3;
				}
				else if ((count == '*' * 2 + '#' * 1 + ' ' * 6) && board[1][2] == '*' && board[0][1] == '*' && board[1][1] == '#')
				{
					board[0][2] = '#';
					goto done3;
				}
				goto nan2;
			}
		done3:
			break;

	}
}
		
//判断游戏的4种结果
//玩家赢返回'*'、电脑赢返回'#'、平局返回'Q'、继续返回'C'
int IsFull()
{
	int i;
	int j;
	for (i = 0;i < ROW;i++)
	{
		for (j = 0;j < COL;j++)
		{
			if (board[i][j] == ' ')
			{
				return 0;
			}
		}
	}
	return 1;
}

char IsWin()
{
	int i;
	int j;
	for (i = 0; i < ROW; i++)
	{
		int count1 = 0;
		int j = 0;
		for (j = 0; j < COL - 1; j++)
		{
			if (board[i][j] != ' ' && board[i][j] == board[i][j + 1])
			{
				count1++;
			}
			if (count1 == ROW - 1)
			{
				return board[i][j];
			}
		}
	}

	//竖列
	for (j = 0; j < COL; j++)
	{
		int count2 = 0;
		for (i = 0; i < ROW - 1; i++)
		{
			if (board[i][j] != ' ' && board[i][j] == board[i + 1][j])
			{
				count2++;
			}
			if (count2 == ROW - 1)
			{
				return board[i][j];
			}
		}
	}

	//对角线
	int count3 = 0;
	for (i = 0; i < ROW - 1; i++)
	{
		if (board[i][i] != ' ' && board[i][i] == board[i + 1][i + 1])
		{
			count3++;
		}
		if (count3 == ROW - 1)
		{
			return board[i][i];
		}
	}

	int count4 = 0;
	for (i = 0; i < ROW - 1; i++)
	{
		if (board[i][ROW - 1 - i] != ' ' && board[i][ROW - 1 - i] == board[i + 1][ROW - 2 - i])
		{
			count4++;
		}
		if (count4 == ROW - 1)
		{
			return board[i][ROW - 1 - i];
		}

	}
	//判断是否平局,平局返回1，否则返回0
	if (IsFull(board) == 1)
	{
		return 'Q';
	}
	
	return 'C';
}